package com.code.splider.common.util;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;


/**
 * @author 王振兴
 *
 */
public class SAXUtil {

	/**
	 * 根据url获取导航栏的名称和对应的链接
	 * @param url
	 * @return
	 */
	public static List<Map<String,String>> getMenuContent(String url){
		Element el= null;
		Elements els= null;
		List<Map<String,String>> retList = null;
		try {
			Document doc = Jsoup.connect(url).header("User-Agent", "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.4; en-US; rv:1.9.2.2) Gecko/20100316 Firefox/3.6.2")
			.header("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:26.0) Gecko/20100101 Firefox/22.0")
			.header("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")
			.ignoreContentType(true).timeout(5000).get();
			el = doc.getElementsByAttributeValue("class", "button-group ds-button-group radius").first();
			if(el != null){
				els = el.select("li");
				retList = new ArrayList<Map<String,String>>();
				for(Element li : els){
					Map<String,String> map = new HashMap<String,String>();
					Element a = li.select("a[href]").first();
					map.put("href", a.attr("href"));
					map.put("name", li.select("a").text().trim());
					retList.add(map);
				}
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return retList;
	}
	
	public static List<Map<String,String>> getTabContent(String url){
		Element el= null;
		Elements elsTr = null;
		Elements elsTd = null;
		List<Map<String,String>> retList = null;
		try {
			Document doc = Jsoup.connect(url).header("User-Agent", "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.4; en-US; rv:1.9.2.2) Gecko/20100316 Firefox/3.6.2")
					.header("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:26.0) Gecko/20100101 Firefox/22.0")
					.header("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")
					.ignoreContentType(true).timeout(5000).get();
			el = doc.getElementsByAttributeValue("class", "live-list-table").first();
			if(el != null){
				elsTr = el.select("tbody tr");
				retList = new ArrayList<Map<String,String>>();
				for(Element e : elsTr){
					Map<String,String> map = new HashMap<String,String>();
					elsTd = e.select("td");
					for(int i=0;i<elsTd.size();i++){
						switch (i) {
						case 0:
							map.put("league", elsTd.get(i).select("a").text().trim());
							break;
						case 2:
							map.put("beginTime", elsTd.get(i).text().trim());
							break;
						case 3:
							Elements teams = elsTd.get(i).select("a");
							for(int j = 0;j<teams.size();j++){
								switch (j) {
								case 0:
									map.put("home_team", teams.get(j).text().trim());
									break;
								case 1:
									map.put("away_team", teams.get(j).text().trim());								
									break;
								case 2:
									map.put("pan_kou", teams.get(j).text().trim());
									Element link  = teams.get(j);
									String str = link .attr("href");
									if(str!=null){
									 String[] hrefs  =	str.split("/");
									 map.put("zhiboId", hrefs[hrefs.length-1]);
									}
									break;

								default:
									break;
								}
							}
//							map.put("team", elsTd.get(i).select("a").text().trim());
							break;
						case 4:
							map.put("isBegin", elsTd.get(i).text().trim());
							break;
						case 5:
							map.put("num1", elsTd.get(i).text().trim());
							break;
						case 6:
							map.put("num2", elsTd.get(i).text().trim());
							break;
						case 7:
							map.put("num3", elsTd.get(i).text().trim());
							break;
						case 8:
							map.put("num4", elsTd.get(i).text().trim());
							break;
						case 9:
							break;
						case 10:
							break;
						default:
							break;
						}
					}
					retList.add(map);
				}
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return retList;
	}
}
